package eproject.mm.com.projectsupervision.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import eproject.mm.com.projectsupervision.PrjApplication;
import eproject.mm.com.projectsupervision.R;
import eproject.mm.com.projectsupervision.widget.IconFontTextview;

public class BaseRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private LayoutInflater mLayoutInflater;
    private Context context;
    private List<Map> lists;

    //建立枚举 2个item 类型
    public enum ITEM_TYPE {
        PLAN,//进度报告
        PROBLEM, //问题上传
        MODIFY,
        CHECK,
        MESSAGE
    }

    public BaseRecyclerViewAdapter(Context context, List<Map> lists){
        this.lists = lists;
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //加载Item View的时候根据不同TYPE加载不同的布局
        if (viewType == ITEM_TYPE.PLAN.ordinal()) {
            return new ItemPlanViewHolder(mLayoutInflater.inflate(R.layout.rv_item_report, parent, false));
        } else if (viewType == ITEM_TYPE.PROBLEM.ordinal()) {
            return new ItemProblemViewHolder(mLayoutInflater.inflate(R.layout.rv_item_problem, parent, false));
        } else if (viewType == ITEM_TYPE.MODIFY.ordinal()) {
            return new ItemModifyViewHolder(mLayoutInflater.inflate(R.layout.rv_item_modify, parent, false));
        } else if (viewType == ITEM_TYPE.CHECK.ordinal()) {
            return new ItemCheckViewHolder(mLayoutInflater.inflate(R.layout.rv_item_check, parent, false));
        } else if (viewType == ITEM_TYPE.MESSAGE.ordinal()) {
            return new ItemMessageViewHolder(mLayoutInflater.inflate(R.layout.rv_item_message, parent, false));
        }
        return new ItemPlanViewHolder(mLayoutInflater.inflate(R.layout.rv_item_report, parent, false));
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        Map item = (Map)lists.get(position);
        if (holder instanceof ItemPlanViewHolder) {
            String unitname = (String)item.get("unitname");
            if(unitname==null)unitname = (String)item.get("supunitname");
            ((ItemPlanViewHolder) holder).tv_person.setText(unitname + " " + (String)item.get("creatorname"));
            String sTime = (String)item.get("createtime");
            if(sTime.length()>16) {
                sTime = sTime.substring(5, 16);
            }
            ((ItemPlanViewHolder) holder).tv_time.setText(sTime);
            ((ItemPlanViewHolder) holder).tv_text.setText((String)item.get("describes"));

            ((ItemPlanViewHolder) holder).tv_projectName.setText((String)item.get("projectname"));
            ((ItemPlanViewHolder) holder).tv_planNodeName.setText((String)item.get("taskname"));
            ((ItemPlanViewHolder) holder).tv_buildUnitName.setText((String)item.get("percentdone")+"%");

            RecyclerView rv_images = ((ItemPlanViewHolder) holder).rv_images;

            String sImagesStr = (String)item.get("newnames2");
            String bImagesStr = (String)item.get("newnames");
            if(sImagesStr != null && !sImagesStr.trim().equals("")){
                String[] sImagesArray = sImagesStr.split(",");
                String[] bImagesArray = bImagesStr.split(",");

                if(sImagesArray!=null){
                    List<String> sImages = new ArrayList<>();
                    List<String> bImages = new ArrayList<>();
                    for(int i=0;i<sImagesArray.length;i++){
                        sImages.add(sImagesArray[i]);

                        bImages.add("http://"+ PrjApplication.getInstance().getServerIp()+":"+PrjApplication.getInstance().getServerPort()+bImagesArray[i]);
                    }
                    BaseRecyclerImageViewAdapter imageViewAdapter = new BaseRecyclerImageViewAdapter(context, sImages, bImages);
                    ((ItemPlanViewHolder) holder).rv_images.setLayoutManager(new GridLayoutManager(context,4));
                    rv_images.getLayoutManager().setAutoMeasureEnabled(true);
                    ((ItemPlanViewHolder) holder).rv_images.setAdapter(imageViewAdapter);
                    ((ItemPlanViewHolder) holder).rv_images.setVisibility(View.VISIBLE);
                }
            }else{
                ((ItemPlanViewHolder) holder).rv_images.setVisibility(View.GONE);
            }
            if(mOnItemClickListener!=null){
                ((ItemPlanViewHolder) holder).rl_item.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v)
                    {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickListener.onItemClick(holder.itemView, pos);
                    }
                });
            }
        } else if (holder instanceof ItemProblemViewHolder) {
            String unitname = (String)item.get("unitname");
            if(unitname==null)unitname = (String)item.get("supunitname");
            ((ItemProblemViewHolder) holder).tv_person.setText(unitname + " " + (String)item.get("creatorname"));
            String sTime = (String)item.get("createtime");
            if(sTime.length()>16) {
                sTime = sTime.substring(5, 16);
            }
            ((ItemProblemViewHolder) holder).tv_time.setText(sTime);
            ((ItemProblemViewHolder) holder).tv_text.setText((String)item.get("describes"));

            ((ItemProblemViewHolder) holder).tv_projectName.setText((String)item.get("projectname"));
            ((ItemProblemViewHolder) holder).tv_planNodeName.setText((String)item.get("taskname"));

            RecyclerView rv_images = ((ItemProblemViewHolder) holder).rv_images;

//            String type = (String)item.get("type")+"";
//            if(type.equals("0")){
//                ((ItemProblemViewHolder) holder).iftv_icon.setTextColor(ContextCompat.getColor(context,R.color.colorProblem));
//            }else{
//                ((ItemProblemViewHolder) holder).iftv_icon.setTextColor(ContextCompat.getColor(context,R.color.colorProblem));
//            }

            String sImagesStr = (String)item.get("newnames2");
            String bImagesStr = (String)item.get("newnames");
            if(sImagesStr != null && !sImagesStr.trim().equals("")){
                String[] sImagesArray = sImagesStr.split(",");
                String[] bImagesArray = bImagesStr.split(",");

                if(sImagesArray!=null){
                    List<String> sImages = new ArrayList<>();
                    List<String> bImages = new ArrayList<>();
                    for(int i=0;i<sImagesArray.length;i++){
                        sImages.add(sImagesArray[i]);

                        bImages.add("http://"+ PrjApplication.getInstance().getServerIp()+":"+PrjApplication.getInstance().getServerPort()+bImagesArray[i]);
                    }
                    BaseRecyclerImageViewAdapter imageViewAdapter = new BaseRecyclerImageViewAdapter(context, sImages, bImages);
                    ((ItemProblemViewHolder) holder).rv_images.setLayoutManager(new GridLayoutManager(context,4));
                    rv_images.getLayoutManager().setAutoMeasureEnabled(true);
                    ((ItemProblemViewHolder) holder).rv_images.setAdapter(imageViewAdapter);
                    ((ItemProblemViewHolder) holder).rv_images.setVisibility(View.VISIBLE);
                }
            }else{
                ((ItemProblemViewHolder) holder).rv_images.setVisibility(View.GONE);
            }
            if(mOnItemClickListener!=null){
                ((ItemProblemViewHolder) holder).rl_item.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v)
                    {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickListener.onItemClick(holder.itemView, pos);
                    }
                });
            }
        } else if (holder instanceof ItemModifyViewHolder) {
            String unitname = (String)item.get("unitname");
            if(unitname==null)unitname = (String)item.get("supunitname");
            ((ItemModifyViewHolder) holder).tv_person.setText(unitname + " " + (String)item.get("creatorname"));
            String sTime = (String)item.get("createtime");
            if(sTime.length()>16) {
                sTime = sTime.substring(5, 16);
            }
            ((ItemModifyViewHolder) holder).tv_time.setText(sTime);
            ((ItemModifyViewHolder) holder).tv_text.setText((String)item.get("describes"));

            ((ItemModifyViewHolder) holder).tv_projectName.setText((String)item.get("projectname"));
            ((ItemModifyViewHolder) holder).tv_planNodeName.setText((String)item.get("taskname"));

            RecyclerView rv_images = ((ItemModifyViewHolder) holder).rv_images;

//            String type = (String)item.get("type")+"";
//            if(type.equals("0")){
//                ((ItemProblemViewHolder) holder).iftv_icon.setTextColor(ContextCompat.getColor(context,R.color.colorProblem));
//            }else{
//                ((ItemProblemViewHolder) holder).iftv_icon.setTextColor(ContextCompat.getColor(context,R.color.colorProblem));
//            }

            String sImagesStr = (String)item.get("newnames2");
            String bImagesStr = (String)item.get("newnames");
            if(sImagesStr != null && !sImagesStr.trim().equals("")){
                String[] sImagesArray = sImagesStr.split(",");
                String[] bImagesArray = bImagesStr.split(",");

                if(sImagesArray!=null){
                    List<String> sImages = new ArrayList<>();
                    List<String> bImages = new ArrayList<>();
                    for(int i=0;i<sImagesArray.length;i++){
                        sImages.add(sImagesArray[i]);

                        bImages.add("http://"+ PrjApplication.getInstance().getServerIp()+":"+PrjApplication.getInstance().getServerPort()+bImagesArray[i]);
                    }
                    BaseRecyclerImageViewAdapter imageViewAdapter = new BaseRecyclerImageViewAdapter(context, sImages, bImages);
                    ((ItemModifyViewHolder) holder).rv_images.setLayoutManager(new GridLayoutManager(context,4));
                    rv_images.getLayoutManager().setAutoMeasureEnabled(true);
                    ((ItemModifyViewHolder) holder).rv_images.setAdapter(imageViewAdapter);
                    ((ItemModifyViewHolder) holder).rv_images.setVisibility(View.VISIBLE);
                }
            }else{
                ((ItemModifyViewHolder) holder).rv_images.setVisibility(View.GONE);
            }
            if(mOnItemClickListener!=null){
                ((ItemModifyViewHolder) holder).rl_item.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v)
                    {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickListener.onItemClick(holder.itemView, pos);
                    }
                });
            }
        } else if (holder instanceof ItemCheckViewHolder) {
            String unitname = (String)item.get("unitname");
            if(unitname==null)unitname = (String)item.get("supunitname");
            ((ItemCheckViewHolder) holder).tv_person.setText(unitname + " " + (String)item.get("creatorname"));
            String sTime = (String)item.get("createtime");
            if(sTime.length()>16) {
                sTime = sTime.substring(5, 16);
            }
            ((ItemCheckViewHolder) holder).tv_time.setText(sTime);
            ((ItemCheckViewHolder) holder).tv_text.setText((String)item.get("describes"));

            ((ItemCheckViewHolder) holder).tv_projectName.setText((String)item.get("projectname"));
            ((ItemCheckViewHolder) holder).tv_planNodeName.setText((String)item.get("taskname"));
            //((ItemCheckViewHolder) holder).tv_buildUnitName.setText((String)item.get("taskname"));

            RecyclerView rv_images = ((ItemCheckViewHolder) holder).rv_images;

            String sImagesStr = (String)item.get("newnames2");
            String bImagesStr = (String)item.get("newnames");
            if(sImagesStr != null && !sImagesStr.trim().equals("")){
                String[] sImagesArray = sImagesStr.split(",");
                String[] bImagesArray = bImagesStr.split(",");

                if(sImagesArray!=null){
                    List<String> sImages = new ArrayList<>();
                    List<String> bImages = new ArrayList<>();
                    for(int i=0;i<sImagesArray.length;i++){
                        sImages.add(sImagesArray[i]);

                        bImages.add("http://"+ PrjApplication.getInstance().getServerIp()+":"+PrjApplication.getInstance().getServerPort()+bImagesArray[i]);
                    }
                    BaseRecyclerImageViewAdapter imageViewAdapter = new BaseRecyclerImageViewAdapter(context, sImages, bImages);
                    ((ItemCheckViewHolder) holder).rv_images.setLayoutManager(new GridLayoutManager(context,4));
                    rv_images.getLayoutManager().setAutoMeasureEnabled(true);
                    ((ItemCheckViewHolder) holder).rv_images.setAdapter(imageViewAdapter);
                    ((ItemCheckViewHolder) holder).rv_images.setVisibility(View.VISIBLE);
                }
            }else{
                ((ItemCheckViewHolder) holder).rv_images.setVisibility(View.GONE);
            }
            if(mOnItemClickListener!=null){
                ((ItemCheckViewHolder) holder).rl_item.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v)
                    {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickListener.onItemClick(holder.itemView, pos);
                    }
                });
            }
        } else if (holder instanceof ItemMessageViewHolder) {
            String unitname = (String)item.get("unitname");
            if(unitname==null)unitname = (String)item.get("supunitname");
            ((ItemMessageViewHolder) holder).tv_person.setText(unitname + " " + (String)item.get("creatorname"));
            String sTime = (String)item.get("createtime");
            if(sTime.length()>16) {
                sTime = sTime.substring(5, 16);
            }
            ((ItemMessageViewHolder) holder).tv_time.setText(sTime);
            ((ItemMessageViewHolder) holder).tv_text.setText((String)item.get("describes"));

//            String type = (String)item.get("type")+"";
//            if(type.equals("0")){
//                ((ItemMessageViewHolder) holder).iftv_icon.setTextColor(ContextCompat.getColor(context,R.color.colorProblem));
//            }else{
//                ((ItemMessageViewHolder) holder).iftv_icon.setTextColor(ContextCompat.getColor(context,R.color.colorProblem));
//            }
            if(mOnItemClickListener!=null){
                ((ItemMessageViewHolder) holder).rl_item.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v)
                    {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickListener.onItemClick(holder.itemView, pos);
                    }
                });
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        Map item = (Map)lists.get(position);
        String dtype = (String)item.get("dtype")+"";
        String type = (String)item.get("type")+"";
        if(dtype.equals("plan")){
            return ITEM_TYPE.PLAN.ordinal();
        }else if(dtype.equals("problem")){
            return ITEM_TYPE.PROBLEM.ordinal();
        }else{
            if(type.equals("0")){
                return ITEM_TYPE.PROBLEM.ordinal();
            }else if(type.equals("1")){
                return ITEM_TYPE.MODIFY.ordinal();
            }else if(type.equals("2")){
                return ITEM_TYPE.CHECK.ordinal();
            }else if(type.equals("3")){
                return ITEM_TYPE.PLAN.ordinal();
            }else if(type.equals("4")){
                return ITEM_TYPE.MESSAGE.ordinal();
            }
        }
        return ITEM_TYPE.PLAN.ordinal();
    }

    @Override
    public int getItemCount() {
        return lists == null ? 0 : lists.size();
    }

    //item1 的ViewHolder
    public static class ItemPlanViewHolder extends RecyclerView.ViewHolder{
        RelativeLayout rl_item;
        TextView tv_person, tv_time, tv_text, tv_projectName, tv_planNodeName, tv_buildUnitName;
        RecyclerView rv_images;

        public ItemPlanViewHolder(View itemView) {
            super(itemView);
            rl_item = (RelativeLayout) itemView.findViewById(R.id.rl_item);
            tv_person = (TextView)itemView.findViewById(R.id.tv_person);
            tv_time = (TextView)itemView.findViewById(R.id.tv_time);
            tv_text = (TextView)itemView.findViewById(R.id.tv_text);

            tv_projectName = (TextView)itemView.findViewById(R.id.tv_projectName);
            tv_planNodeName = (TextView)itemView.findViewById(R.id.tv_planNodeName);
            tv_buildUnitName = (TextView)itemView.findViewById(R.id.tv_buildUnitName);

            rv_images = (RecyclerView)itemView.findViewById(R.id.rv_images);
        }
    }
    //item2 的ViewHolder
    public static class ItemProblemViewHolder extends RecyclerView.ViewHolder{
        RelativeLayout rl_item;
        TextView tv_person, tv_time, tv_text, tv_projectName, tv_planNodeName, tv_buildUnitName;
        IconFontTextview iftv_icon;
        RecyclerView rv_images;

        public ItemProblemViewHolder(View itemView) {
            super(itemView);
            rl_item = (RelativeLayout) itemView.findViewById(R.id.rl_problem_item);
            iftv_icon = (IconFontTextview)itemView.findViewById(R.id.iftv_icon);
            tv_person = (TextView)itemView.findViewById(R.id.tv_person);
            tv_time = (TextView)itemView.findViewById(R.id.tv_time);
            tv_text = (TextView)itemView.findViewById(R.id.tv_text);

            tv_projectName = (TextView)itemView.findViewById(R.id.tv_projectName);
            tv_planNodeName = (TextView)itemView.findViewById(R.id.tv_planNodeName);
            tv_buildUnitName = (TextView)itemView.findViewById(R.id.tv_buildUnitName);

            rv_images = (RecyclerView)itemView.findViewById(R.id.rv_images);
        }
    }
    public static class ItemModifyViewHolder extends RecyclerView.ViewHolder{
        RelativeLayout rl_item;
        TextView tv_person, tv_time, tv_text, tv_projectName, tv_planNodeName, tv_buildUnitName;
        IconFontTextview iftv_icon;
        RecyclerView rv_images;

        public ItemModifyViewHolder(View itemView) {
            super(itemView);
            rl_item = (RelativeLayout) itemView.findViewById(R.id.rl_item);
            iftv_icon = (IconFontTextview)itemView.findViewById(R.id.iftv_icon);
            tv_person = (TextView)itemView.findViewById(R.id.tv_person);
            tv_time = (TextView)itemView.findViewById(R.id.tv_time);
            tv_text = (TextView)itemView.findViewById(R.id.tv_text);

            tv_projectName = (TextView)itemView.findViewById(R.id.tv_projectName);
            tv_planNodeName = (TextView)itemView.findViewById(R.id.tv_planNodeName);
            tv_buildUnitName = (TextView)itemView.findViewById(R.id.tv_buildUnitName);

            rv_images = (RecyclerView)itemView.findViewById(R.id.rv_images);
        }
    }
    public static class ItemCheckViewHolder extends RecyclerView.ViewHolder{
        RelativeLayout rl_item;
        TextView tv_person, tv_time, tv_text, tv_projectName, tv_planNodeName, tv_buildUnitName;
        IconFontTextview iftv_icon;
        RecyclerView rv_images;

        public ItemCheckViewHolder(View itemView) {
            super(itemView);
            rl_item = (RelativeLayout) itemView.findViewById(R.id.rl_item);
            iftv_icon = (IconFontTextview)itemView.findViewById(R.id.iftv_icon);
            tv_person = (TextView)itemView.findViewById(R.id.tv_person);
            tv_time = (TextView)itemView.findViewById(R.id.tv_time);
            tv_text = (TextView)itemView.findViewById(R.id.tv_text);

            tv_projectName = (TextView)itemView.findViewById(R.id.tv_projectName);
            tv_planNodeName = (TextView)itemView.findViewById(R.id.tv_planNodeName);
            tv_buildUnitName = (TextView)itemView.findViewById(R.id.tv_buildUnitName);

            rv_images = (RecyclerView)itemView.findViewById(R.id.rv_images);
        }
    }
    public static class ItemMessageViewHolder extends RecyclerView.ViewHolder{
        RelativeLayout rl_item;
        TextView tv_person, tv_time, tv_text;
        IconFontTextview iftv_icon;
        RecyclerView rv_images;

        public ItemMessageViewHolder(View itemView) {
            super(itemView);
            rl_item = (RelativeLayout) itemView.findViewById(R.id.rl_item);
            iftv_icon = (IconFontTextview)itemView.findViewById(R.id.iftv_icon);
            tv_person = (TextView)itemView.findViewById(R.id.tv_person);
            tv_time = (TextView)itemView.findViewById(R.id.tv_time);
            tv_text = (TextView)itemView.findViewById(R.id.tv_text);

            rv_images = (RecyclerView)itemView.findViewById(R.id.rv_images);
        }
    }
    public void addItems(List list){
        int pos = lists.size();
        lists.addAll(pos,list);

        notifyItemInserted(pos);
    }

    public void setItems(List list){
        lists = list;

        notifyDataSetChanged();
    }

    BaseRecyclerViewAdapter.onItemClickListener mOnItemClickListener;
    public interface onItemClickListener {
        public void onItemClick(View view, int position);
    }
    public void setOnItemClickLitsener(BaseRecyclerViewAdapter.onItemClickListener mOnItemClickLitsener) {
        mOnItemClickListener = mOnItemClickLitsener;
    }
    public Object getItemAt(int pos){
        return lists.get(pos);
    }
}